package com.connection.second.wallpaper.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.connection.second.wallpaper.domain.entity.PaperAlbum;
import com.connection.second.wallpaper.domain.entity.PaperClassify;
import com.connection.second.wallpaper.domain.entity.PaperWall;
import com.connection.second.wallpaper.domain.vo.PaperAlbumVo;
import com.connection.second.wallpaper.domain.vo.PaperWallVo;
import com.connection.second.wallpaper.domain.vo.UserPostingStatistics;
import com.connection.second.wallpaper.mapper.PaperAlbumMapper;
import com.connection.second.wallpaper.mapper.PaperClassifyMapper;
import com.connection.second.wallpaper.mapper.PaperWallMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.connection.second.wallpaper.mapper.PaperUserPostingMapper;
import com.connection.second.wallpaper.domain.entity.PaperUserPosting;
import com.connection.second.wallpaper.service.IPaperUserPostingService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.connection.second.common.utils.SecurityUtils.getUsername;

/**
 * 发布审核Service业务层处理
 * 
 * @author FL
 * @date 2024-11-15
 */
@Service
@Slf4j
public class PaperUserPostingServiceImpl implements IPaperUserPostingService 
{
    @Resource
    private PaperUserPostingMapper paperUserPostingMapper;
    @Resource
    private PaperWallMapper paperWallMapper;
    @Resource
    private PaperAlbumMapper paperAlbumMapper;
    @Resource
    private PaperClassifyMapper paperClassifyMapper;

    /**
     * 查询发布审核
     * 
     * @param id 发布审核主键
     * @return 发布审核
     */
    @Override
    public PaperUserPosting selectPaperUserPostingById(Long id)
    {
        return paperUserPostingMapper.selectPaperUserPostingById(id);
    }

    /**
     * 查询发布审核列表
     * 
     * @param paperUserPosting 发布审核
     * @return 发布审核
     */
    @Override
    public List<PaperUserPosting> selectPaperUserPostingList(PaperUserPosting paperUserPosting)
    {
        return paperUserPostingMapper.selectPaperUserPostingList(paperUserPosting);
    }

    /**
     * 新增发布审核
     * 
     * @param paperUserPosting 发布审核
     * @return 结果
     */
    @Override
    public int insertPaperUserPosting(PaperUserPosting paperUserPosting)
    {
        return paperUserPostingMapper.insertPaperUserPosting(paperUserPosting);
    }

    /**
     * 修改发布审核
     * 
     * @param paperUserPosting 发布审核
     * @return 结果
     */
    @Override
    public int updatePaperUserPosting(PaperUserPosting paperUserPosting)
    {
        return paperUserPostingMapper.updatePaperUserPosting(paperUserPosting);
    }

    /**
     * 批量删除发布审核
     * 
     * @param ids 需要删除的发布审核主键
     * @return 结果
     */
    @Override
    public int deletePaperUserPostingByIds(Long[] ids)
    {
        return paperUserPostingMapper.deletePaperUserPostingByIds(ids);
    }

    /**
     * 删除发布审核信息
     * 
     * @param id 发布审核主键
     * @return 结果
     */
    @Override
    public int deletePaperUserPostingById(Long id)
    {
        return paperUserPostingMapper.deletePaperUserPostingById(id);
    }

    /**
     * 发布壁纸
     * @param paperWall
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userPostWallpaper(PaperWall paperWall) {
        // 检查壁纸是否已存在
        PaperWall existingWall = paperWallMapper.selectPaperWallById(paperWall.getId());

        if (!ObjectUtil.isNull(existingWall)) {
            // 如果已存在，则更新
            BeanUtils.copyProperties(paperWall, existingWall);
            paperWallMapper.updatePaperWall(existingWall);
            log.info("更新壁纸成功，wallId={}", existingWall.getId());
    //        修改用户发布记录
            PaperUserPosting paperUserPosting = new PaperUserPosting();
            paperUserPosting.setType(1);
            paperUserPosting.setItemId(existingWall.getId());
            paperUserPosting.setUserId(existingWall.getUserId());
            paperUserPosting = paperUserPostingMapper.selectPaperUserPostingList(paperUserPosting).get(0);
            return paperUserPostingMapper.updatePaperUserPosting(paperUserPosting);
        } else {
            // 如果不存在，则插入新壁纸
            paperWallMapper.insertPaperWall(paperWall);
            Long wallId = paperWall.getId();
            log.info("发布壁纸成功，wallId={}", wallId);

            // 插入用户发布记录
            PaperUserPosting paperUserPosting = new PaperUserPosting();
            paperUserPosting.setType(1);
            paperUserPosting.setItemId(wallId);
            paperUserPosting.setStatus(0);
            paperUserPosting.setUserId(paperWall.getUserId());

            return paperUserPostingMapper.insertPaperUserPosting(paperUserPosting);
        }
    }


    /**
     * 发布专辑
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userPostAlbum(PaperAlbum paperAlbum) {
        // 检查专辑是否已存在
        PaperAlbum existingAlbum = paperAlbumMapper.selectPaperAlbumById(paperAlbum.getId());

        if (!ObjectUtil.isNull(existingAlbum) ) {
            // 如果存在，则更新
            BeanUtils.copyProperties(paperAlbum, existingAlbum, "id", "createTime"); // 复制属性，排除id和createTime
            paperAlbumMapper.updatePaperAlbum(existingAlbum);
            log.info("更新专辑成功，albumId={}", existingAlbum.getId());
            PaperUserPosting paperUserPosting = new PaperUserPosting();
            paperUserPosting.setUserId(existingAlbum.getUserId());
            paperUserPosting.setItemId(existingAlbum.getId());
            paperUserPosting.setType(2);
            paperUserPosting = paperUserPostingMapper.selectPaperUserPostingList(paperUserPosting).get(0);
            return paperUserPostingMapper.updatePaperUserPosting(paperUserPosting);
        } else {
            // 如果不存在，则插入新专辑
            paperAlbumMapper.insertPaperAlbum(paperAlbum);
            Long albumId = paperAlbum.getId();
            log.info("发布专辑成功，albumId={}", albumId);

            // 插入用户发布记录
            PaperUserPosting paperUserPosting = new PaperUserPosting();
            paperUserPosting.setType(2);
            paperUserPosting.setItemId(albumId);
            paperUserPosting.setStatus(0);
            paperUserPosting.setUserId(paperAlbum.getUserId());
            return paperUserPostingMapper.insertPaperUserPosting(paperUserPosting);
        }

    }


    @Override
    public List<UserPostingStatistics> getUserPostStatistics() {
        return paperUserPostingMapper.getUserPostStatistics();
    }

    /**
     * 查询用户发布的壁纸列表
     * @param paperUserPosting
     * @return
     */
    @Override
    public List<PaperWallVo> selectUserWallpaperList(PaperUserPosting paperUserPosting) {
        List<PaperWallVo> list = new ArrayList<>();
        List<PaperUserPosting> postings = paperUserPostingMapper.selectPaperUserPostingList(paperUserPosting);
        for (PaperUserPosting posting : postings) {
            PaperWallVo vo = convertPostingToVo(posting);
            if (ObjectUtil.isNull(vo)) {
                break;
            }
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<PaperAlbumVo> selectUserAlbumList(PaperUserPosting paperUserPosting) {
        List<PaperAlbumVo> list = new ArrayList<>();
        List<PaperUserPosting> postings = paperUserPostingMapper.selectPaperUserPostingList(paperUserPosting);
        for (PaperUserPosting posting : postings) {
            PaperAlbumVo vo = convertPostingToAlbumVo(posting);
            if (ObjectUtil.isNull(vo)) {
                break;
            }
            list.add(vo);
        }
        return list;
    }

    @Override
    public PaperAlbumVo selectAlbumById(Long id) {
        PaperAlbumVo vo = new PaperAlbumVo();
        PaperAlbum album = paperAlbumMapper.selectPaperAlbumById(id);
        BeanUtils.copyProperties(album, vo);
        String className = getClassName(album.getClassId());
        vo.setClassName(className);
        return vo;
    }

    @Override
    public PaperWallVo selectWallpaperById(Long id) {
        PaperWallVo vo = new PaperWallVo();
        PaperWall wall = paperWallMapper.selectPaperWallById(id);
        BeanUtils.copyProperties(wall, vo);
        String className = getClassName(wall.getClassId());
        vo.setClassName(className);
        return vo;
    }


    // 转换发布项为PaperWallVo
    private PaperWallVo convertPostingToVo(PaperUserPosting posting) {
        PaperWallVo vo = new PaperWallVo();
        PaperWall wall = paperWallMapper.selectPaperWallById(posting.getItemId());
        if (ObjectUtil.isNull(wall)) {
            return null;
        }
        BeanUtils.copyProperties(wall, vo);
        String className = getClassName(wall.getClassId());
        vo.setClassName(className);
        return vo;
    }

    // 转换发布项为PaperAlbumVo
    private PaperAlbumVo convertPostingToAlbumVo(PaperUserPosting posting) {
        PaperAlbumVo vo = new PaperAlbumVo();
        PaperAlbum album = paperAlbumMapper.selectPaperAlbumById(posting.getItemId());
        if (ObjectUtil.isNull(album)) {
            return null;
        }
        BeanUtils.copyProperties(album, vo);
        String className = getClassName(album.getClassId());
        vo.setClassName(className);
        return vo;
    }

    // 获取分类名称
    private String getClassName(Long classId) {
        if (classId != null) {
            PaperClassify classify = paperClassifyMapper.selectPaperClassifyById(classId);
            return classify != null ? classify.getName() : "未分类";
        } else {
            return "未分类";
        }
    }

}
